//
//  UIColor+HexColor.swift
//  Swift-Animations
//
//  Created by YouXianMing on 16/8/14.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#if os(iOS) || os(tvOS)
    
    import UIKit
    typealias HexColor = UIColor
    
#else
    
    import Cocoa
    typealias HexColor = NSColor
    
#endif

private extension Int {
    
    func duplicate4bits() -> Int {
        
        return (self << 4) + self
    }
}


extension HexColor {
    
    // MARK: class func.
    
    class func Hex(hex6 : Int) -> HexColor {
        
        if (0x000000 ... 0xFFFFFF) ~= hex6 {
            
            return HexColor(hex6: hex6, alpha: 1)!
            
        } else {
            
            return HexColor.whiteColor()
        }
    }
    
    class func Hex6(hex6 : Int, alpha : Float) -> HexColor {
    
        if (0x000000 ... 0xFFFFFF) ~= hex6 {
        
            return HexColor(hex6: hex6, alpha: alpha)!
            
        } else {
        
            return HexColor.whiteColor()
        }
    }
    
    class func Hex6(hex6 : Int) -> HexColor {
        
        if (0x000000 ... 0xFFFFFF) ~= hex6 {
            
            return HexColor(hex6: hex6, alpha: 1)!
            
        } else {
            
            return HexColor.whiteColor()
        }
    }
    
    class func Hex3(hex3 : Int, alpha : Float) -> HexColor {
        
        if (0x000 ... 0xFFF) ~= hex3 {
            
            return HexColor(hex3: hex3, alpha: alpha)!
            
        } else {
            
            return HexColor.whiteColor()
        }
    }
    
    class func Hex3(hex3 : Int) -> HexColor {
        
        if (0x000 ... 0xFFF) ~= hex3 {
            
            return HexColor(hex3: hex3, alpha: 1)!
            
        } else {
            
            return HexColor.whiteColor()
        }
    }
    
    // MARK: Private func.
    
    convenience init?(hex3: Int, alpha: Float) {
        
        self.init(red   : CGFloat(((hex3 & 0xF00) >> 8).duplicate4bits()) / 255.0,
                  green : CGFloat(((hex3 & 0x0F0) >> 4).duplicate4bits()) / 255.0,
                  blue  : CGFloat(((hex3 & 0x00F) >> 0).duplicate4bits()) / 255.0,
                  alpha : CGFloat(alpha))
    }
    
    convenience init?(hex6: Int, alpha: Float) {
        
        self.init(red   : CGFloat((hex6 & 0xFF0000) >> 16) / 255.0,
                  green : CGFloat((hex6 & 0x00FF00) >> 8)  / 255.0,
                  blue  : CGFloat((hex6 & 0x0000FF) >> 0)  / 255.0,
                  alpha : CGFloat(alpha))
    }
    
    class func colorWithHexString(hex: String) -> UIColor {
    
                 var cString: String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercaseString
        
                 if (cString.hasPrefix("#")) {
                         cString = (cString as NSString).substringFromIndex(1)
                     }
        
                 if (cString.characters.count != 6) {
                         return UIColor.grayColor()
                     }
        
                 let rString = (cString as NSString).substringToIndex(2)
                 let gString = ((cString as NSString).substringFromIndex(2) as NSString).substringToIndex(2)
                 let bString = ((cString as NSString).substringFromIndex(4) as NSString).substringToIndex(2)
        
                 var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
               NSScanner(string: rString).scanHexInt(&r)
                NSScanner(string: gString).scanHexInt(&g)
                 NSScanner(string: bString).scanHexInt(&b)
        
                return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
             }

    
    
//    class func colorWithHexString (hex:String)
//        -> UIColor {
//            
//            var cString:String
//                = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercaseString
//            
//            
//            
//            if (cString.hasPrefix("#"))
//            {
//                
//                cString = cString.substringFromIndex(1)
//                
//            }
//            
//            
//            
//            if (countElements(cString)
//                !=
//                6) {
//                
//                return UIColor.grayColor()
//                
//            }
//            
//            
//            
//            var rString
//                = cString.substringToIndex(2)
//            
//            var gString
//                = cString.substringFromIndex(2).substringToIndex(2)
//            
//            var bString
//                = cString.substringFromIndex(4).substringToIndex(2)
//            
//            
//            
//            var r:CUnsignedInt 
//                = 0, g:CUnsignedInt
//                    = 
//            0, b:CUnsignedInt =
//            0;
//            
//            NSScanner.scannerWithString(rString).scanHexInt(&r)
//            
//            NSScanner.scannerWithString(gString).scanHexInt(&g)
//            
//            NSScanner.scannerWithString(bString).scanHexInt(&b)
//            
//            
//            
//            return UIColor(red:Float(r) /
//                255.0, green:
//                Float(g) 
//                    / 255.0, blue:
//                Float(b) 
//                    / 255.0, alpha:
//                Float(1))
//            
//    }


}

